﻿using AutoMapper;
using RestApp.BusinessLogic;
using RestApp.UI.Core;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

using models = RestApp.UI.Models;

namespace RestApp.UI.Controllers
{
    [Authorize]
    public class ReportsController : Controller
    {
        IBusinessFactory _factory;
        public ReportsController(IBusinessFactory factory)
        {
            _factory = factory;
        }

        public ActionResult WorkTimeForUser()
        {
            return View(Mapper.Map<IEnumerable<models.User>>(_factory.UsersLogic.GetAllFilteredByLoginCredentials(User.Identity.Name)));
        }

        public ActionResult WorkTimeForUserParameters(int idUser)
        {
            var user = _factory.UsersLogic.GetById(idUser);
            var mappedUser = Mapper.Map<models.User>(user);
            return View(mappedUser);
        }

        public ActionResult WorkTimeForUserReport(DateTime startPeriod, DateTime endPeriod, int userId)
        {
            ReportsLogic reports = new ReportsLogic();
            var report = reports.GetWorkTimeReport(startPeriod, endPeriod, userId);
            return View(report);
        }

        public void WorkTimeForUserExport(DateTime startPeriod, DateTime endPeriod, int userId)
        {
            ReportsLogic reports = new ReportsLogic();
            var report = reports.GetWorkTimeReport(startPeriod, endPeriod, userId);
            string name = string.Format("{0}({1}-{2}).xlsx", report.FullName, report.StartPeriod.ToShortDateString(), report.EndPeriod.ToShortDateString());
            ExcelCreator.ExportReportToExcel(report, name, Response);
        }
        
        [Authorize(Roles="User")]
        public ActionResult BigReport()
        {
            string currentLogin = User.Identity.Name;
            ReportsLogic reports = new ReportsLogic();
            var report = reports.GetBigUserReport(currentLogin).OrderByDescending(t => t.Year);
            return View(report);
        }

        [Authorize(Roles = "User")]
        public ActionResult BigReportDetails(int month, int year)
        {
            string currentLogin = User.Identity.Name;
            ReportsLogic reports = new ReportsLogic();
            var details = reports.BigDetails(currentLogin, month, year);
            return View(details);
        }
    }
}